package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.DynamicRelayDetailBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.beiing.tianshuai.tianshuai.util.SpanStringUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by magikliu on 2017/8/24.
 */

public class DynamicRelayComment_RVAdapter extends RecyclerView.Adapter<DynamicRelayComment_RVAdapter.mViewHolder> implements View.OnClickListener {

    private Context mContext;
    List<DynamicRelayDetailBean.DataBean.CommentBean> mData = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private OnItemClickListener mOnItemClickListener;
    private OnItemViewClickListener mOnItemViewClickListener;
    private OnChildItemClickListener mOnChildItemClickListener;
    private OnTextClickListener mOnTextClickListener;
    private CommentRelayReply_RVAdapter mAdapter;
    private Drawable drawablePraise, drawableUnPraise;

    public DynamicRelayComment_RVAdapter(Context mContext, List<DynamicRelayDetailBean.DataBean.CommentBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        this.layoutInflater = LayoutInflater.from(mContext);
        drawablePraise = mContext.getResources().getDrawable(R.mipmap.praise_red_solid);
        drawableUnPraise = mContext.getResources().getDrawable(R.mipmap.praise_gray_line);
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_dynamic_comment, parent, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder holder, final int position) {
        final int i = mData.size() - position - 1;
        Glide.with(mContext)
                .load(TextUtils.isEmpty(
                        mData.get(position).getPhoto()) ?
                        R.mipmap.default_avatar :
                        HttpRequestConstant.USER_PIC_HEAD + mData.get(position).getPhoto())
                .into(holder.dynamicCommentHead);
        holder.dynamicCommentUsername.setText(mData.get(position).getName());
        holder.dynamicCommentUnit.setText(mData.get(position).getUnit()+" ·");
//        holder.dynamicCommentContent.setText(Html.fromHtml(mData.get(position).getContent()+""));
        try {
            String contentDecode = URLDecoder.decode(mData.get(position).getContent(), "UTF-8");
            holder.dynamicCommentContent.setText(SpanStringUtils.getEmotionContent(mContext, holder.dynamicCommentContent, contentDecode));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.dynamicCommentPraise.setText(mData.get(position).getThing()+"");
        holder.dynamicCommentTime.setText(getTime(mData.get(position).getCreateTime()));
        if (mData.get(position).getIs_thing() == 1){
            holder.dynamicCommentPraise.setCompoundDrawablesWithIntrinsicBounds(drawablePraise, null, null, null);
        } else if (mData.get(position).getIs_thing() == 0){
            holder.dynamicCommentPraise.setCompoundDrawablesWithIntrinsicBounds(drawableUnPraise, null, null, null);
        }

        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        holder.replyRecyclerView.setLayoutManager(manager);
        holder.replyRecyclerView.setNestedScrollingEnabled(false);
        mAdapter = new CommentRelayReply_RVAdapter(mContext, mData.get(position).getReply());
        holder.replyRecyclerView.setAdapter(mAdapter);

        mAdapter.setTextClickListener(new CommentRelayReply_RVAdapter.TextClickListener() {
            @Override
            public void onTextClick(View view, int itemposition, int tvposition) {
                if (mOnTextClickListener != null){
                    mOnTextClickListener.onTextClick(view, position, itemposition, tvposition);
                }
            }
        });
        mAdapter.setOnItemClickListener(new CommentRelayReply_RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int childPosition) {
                if (mOnChildItemClickListener != null){
                    mOnChildItemClickListener.onChildItemClick(view, position, childPosition);
                }
            }
        });

        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);       //条目点击事件
        holder.dynamicCommentLlHead.setTag(position);
        holder.dynamicCommentLlHead.setOnClickListener(this);   //头像点击事件
        holder.dynamicCommentPraise.setTag(position);
        holder.dynamicCommentPraise.setOnClickListener(this);   //点赞点击事件
//        holder.dynamicCommentMore.setTag(position);
//        holder.dynamicCommentMore.setOnClickListener(new View.OnClickListener() {       //更多
//            @Override
//            public void onClick(View view) {
//                mOnItemViewClickListener.onMoreClick(view, holder.getAdapterPosition(), mData.get(position));
//            }
//        });

        holder.replyRecyclerView.setTag(position);
        holder.replyRecyclerView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 动态删除
     * @param position
     */
    public void removeItem(int position){
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public void removeChild(int position, int childPosition){
        mAdapter.removeItem(childPosition);
    }
    @Override
    public void onClick(View view) {
        //条目子View点击事件
        if (mOnItemViewClickListener != null){
            mOnItemViewClickListener.onItemViewClick(view, (int) view.getTag());
        }
        //条目点击事件
        if (mOnItemClickListener != null){
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.dynamic_comment_ll_head)
        LinearLayout dynamicCommentLlHead;
        @BindView(R.id.dynamic_comment_head)
        CircleImageView dynamicCommentHead;
        @BindView(R.id.dynamic_comment_username)
        TextView dynamicCommentUsername;
        @BindView(R.id.dynamic_comment_unit)
        TextView dynamicCommentUnit;
//        @BindView(R.id.dynamic_comment_more)
//        ImageView dynamicCommentMore;
        @BindView(R.id.dynamic_comment_content)
        TextView dynamicCommentContent;
        @BindView(R.id.dynamic_comment_praise)
        TextView dynamicCommentPraise;
        @BindView(R.id.dynamic_comment_time)
        TextView dynamicCommentTime;
        @BindView(R.id.comment_reply_recycler)
        RecyclerView replyRecyclerView;
        public mViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 条目点击事件
     */
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    /**
     * 条目子View点击事件
     */
    public interface OnItemViewClickListener{
        void onItemViewClick(View view, int position);

        void onMoreClick(View view, int position, DynamicRelayDetailBean.DataBean.CommentBean bean);
    }
    public void setOnItemViewClickListener(OnItemViewClickListener listener){
        this.mOnItemViewClickListener = listener;
    }

    //嵌套回复条目点击事件
    public interface OnChildItemClickListener{
        void onChildItemClick(View view, int position, int childPosition);
    }

    public void setOnChildItemClickListener(OnChildItemClickListener listener){
        this.mOnChildItemClickListener = listener;
    }

    //TextView部分文字点击事件
    public interface OnTextClickListener{
        void onTextClick(View view, int position, int itemposition, int textPosition);
    }

    public void setOnTextClickListener(OnTextClickListener listener){
        this.mOnTextClickListener = listener;
    }

    private String getTime(String timeStamp){
        long publishTime = Long.parseLong(timeStamp);
        long currentTime = new Date().getTime() / 1000;
        long timeDifference = currentTime - publishTime;
        long timeDifferenceHour = timeDifference / 3600;
        if (timeDifference / 60 < 1){
            return "刚刚";
        } else if (timeDifferenceHour < 1){
            return (timeDifference / 60) + "分钟前";
        } else if (timeDifferenceHour < 24){
            return timeDifferenceHour + "小时前";
        } else if (timeDifferenceHour / 24 < 30){
            return (timeDifferenceHour / 24) + "天前";
        } else {
            return DateUtil.getStrTime(timeStamp);
        }
    }
}
